package com.wk.controller.teacher;

import com.wk.pojo.Friends;
import com.wk.pojo.Messages;
import com.wk.pojo.User;
import com.wk.service.Courseservice;
import com.wk.service.Talkservice;
import com.wk.service.Userinfoservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: thesis
 * @description: 教师交流控制类
 * @author: 王侃
 * @create: 2020-03-26 11:36
 **/
@Controller
@RequestMapping("/tea")
public class Teachatcontroller {
    @Autowired
    Userinfoservice userinfoservice;
    @Autowired
    Talkservice talkservice;
    @Autowired
    Courseservice courseservice;

    //跳转到好友搜索
    @RequestMapping("/friends/search")
    public String friend_search(String username, Model model)
    {
        List<User> users=new ArrayList<>();
            users=userinfoservice.selectbyusername_friends(username);
            model.addAttribute("users",users);
        return "teacher/chat/friends_search";
    }

    //跳转到我的好友列表
    @RequestMapping("/friends/list")
    public String friends_list(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Friends> friends=userinfoservice.selectbyallmy_friends(user.getId());
        model.addAttribute("friends",friends);
        return "teacher/chat/friends_list";
    }

    //添加为自己的好友
    @RequestMapping("/friends/add/{id}")
    public String friends_add(HttpSession session, @PathVariable("id")Integer id)
    {
        User me=(User)session.getAttribute("loginuser");
        //先查找是否已经是好友
        User he=userinfoservice.selectbyuserid(id);
        //如果还没有加好友，将自动添加好友，1，把他加为我的好友
        Friends flag=userinfoservice.selectbyme_his_friend(me.getId(),id);
        if (flag!=null)
        {
        }
        else {
            Friends friends = new Friends();
            friends.setMy_id(me.getId());
            friends.setMy_name(me.getUsername());
            friends.setHis_id(he.getId());
            friends.setHis_name(he.getUsername());
            //两个用户互加好友
            userinfoservice.insertintoFriends(friends);
        }
        //如果还没有加好友，将自动添加好友，1，把我加为他的好友
        Friends flag2=userinfoservice.selectbyme_his_friend(id,me.getId());
        if (flag2!=null)
        {
        }
        else {
            //两个用户互加好友
            Friends friends = new Friends();
            friends.setMy_id(id);
            friends.setMy_name(he.getUsername());
            friends.setHis_id(me.getId());
            friends.setHis_name(me.getUsername());
            userinfoservice.insertintoFriends(friends);
        }

        return "redirect:/tea/friends/search";
    }

    //跳转到单独聊天界面
    @RequestMapping("/chat_one/{id}")
    public String chat(@PathVariable("id") Integer id, Model model, HttpSession session)
    {
        User me=(User)session.getAttribute("loginuser");
        if(me.getId()!=id)
        {
            User he=userinfoservice.selectbyuserid(id);
            //如果还没有加好友，将自动添加好友，1，把他加为我的好友
            Friends flag=userinfoservice.selectbyme_his_friend(me.getId(),id);
            if (flag!=null)
            {
            }
            else {
                Friends friends = new Friends();
                friends.setMy_id(me.getId());
                friends.setMy_name(me.getUsername());
                friends.setHis_id(he.getId());
                friends.setHis_name(he.getUsername());
                //两个用户互加好友
                userinfoservice.insertintoFriends(friends);
            }
            //如果还没有加好友，将自动添加好友，1，把我加为他的好友
            Friends flag2=userinfoservice.selectbyme_his_friend(id,me.getId());
            if (flag2!=null)
            {
            }
            else {
                //两个用户互加好友
                Friends friends = new Friends();
                friends.setMy_id(id);
                friends.setMy_name(he.getUsername());
                friends.setHis_id(me.getId());
                friends.setHis_name(me.getUsername());
                userinfoservice.insertintoFriends(friends);
            }
            //跳转到聊天界面
            User user=talkservice.selectbyiduser(id);
            model.addAttribute("user",user);
            return "teacher/chat/chat_one";
        }
        else {
            return "student/chat/chat_all";
        }
    }

    //跳转到留言面板
    @RequestMapping("/message/{id}")
    public String message(@PathVariable("id") Integer others_id, HttpSession session,Model model)
    {
        //获得用户的session信息
        User user=(User)session.getAttribute("loginuser");
        //获得用户与对方聊天时的记录
        List<Messages> messages=talkservice.selectbyallmessages(user.getId(),others_id);
        model.addAttribute("messages",messages);
        //获得对方的基本信息
        User others=talkservice.selectbyiduser(others_id);
        model.addAttribute("others",others);
        return "teacher/chat/Message";
    }

    //删除自己的留言
    @RequestMapping("/message/delete/{id}/{others_id}")
    public String message_delete(@PathVariable("id")Integer id,@PathVariable("others_id")Integer others_id)
    {
        talkservice.deletbyidmessage(id);
        return "redirect:/tea/message/"+others_id;
    }

    //完成留言并且存入数据库
    @RequestMapping("/message/submit")
    public String message_submit(Messages messages, MultipartFile msg_file)
    {
        if (msg_file.getOriginalFilename().equals(""))
        {
            System.out.println("文件为空");
        }
        else {
            //上传至阿里云oss并且获得路径
            String my_file=courseservice.upload(msg_file);
            messages.setMy_file(my_file);
        }
        messages.setCreatetime(new Timestamp(new Date().getTime()));
        talkservice.insertintoMessages(messages);
        return "redirect:/tea/message/"+messages.getReceiver_id();
    }
}
